
$(function(){
	var height=(window.parent.getCenterHeight()-$(".btnCss").height()-80)/2;
	$("#cpu ").css('height',height);
	$("#ram").css('height',height);
	$("#disk").css('height',height);
	$("#netWork").css('height',height);
	var cpuChart = echarts.init(document.getElementById('cpu'));
	var ramChart = echarts.init(document.getElementById('ram'));
	var diskChart = echarts.init(document.getElementById('disk'));
	var netWorkChart = echarts.init(document.getElementById('netWork'));
	
	var now = new Date();
	var oneS = 1000;
	var fiveM = 1000 * 60 * 5;//以五分钟为一个跨度
	var newM = 00;
	var timeData = [],
	cpuData = [],
	ramData = [],
	diskData = [],
	netWorkData = [];
	for(var i=fiveM /1000;i>0;i-=5){
		timeData.push(formatDate(now,i));
		cpuData.push({value:0});
		ramData.push({value:0});
		diskData.push({value:0});
		netWorkData.push({value:0});
	}
	

	//格式化日期 yyyy/MM/dd hh:mm
	function formatDate(date,i){
		date = new Date(date - i*oneS);
		return  (date.getMonth()+1) + '/' + date.getDate() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds();
	}
	
	function getCpuUsage() {
	    $.ajax({
			type:'get',
			url:$Path+'/admin/voicePlatformManage/getSystemUsage',
			//async:false,
			success:function(value){
				cpuData.shift();
				ramData.shift();
				diskData.shift();
				netWorkData.shift();
				timeData.shift();
				
				cpuData.push({value:(value.cpuUsageRate).toFixed(4)});
				ramData.push({value:(value.memUsageRate).toFixed(4)});
				diskData.push({value:(value.diskUsageRate).toFixed(4)});
				netWorkData.push({value:(value.netWorkUsageRate).toFixed(4)});
				timeData.push(formatDate(new Date(),0));
			}
		})
	}
	function getOption(type){
		var titleText = '',
			tooltip = '',
			typeData = [];
			
		switch (type) {
		case 'cpu':
			titleText = 'CPU监控图';
			seriesName = '/ cpu使用率'
			typeData = cpuData;
			break;
		case 'ram':
			titleText = '内存监控图';
			seriesName = '/ 内存使用率'
			typeData = ramData;
			break;
		case 'disk':
			titleText = '硬盘监控图';
			seriesName = '/ 硬盘使用率'
			typeData = diskData;
			break;	
		case 'netWork':
			titleText = '网络流量监控图';
			seriesName = '/ 网络流量使用率'
			typeData = 	netWorkData;
			break;
		}
		
		var option = {
		    title: {
		    	x:'center',
		        y:'bottom',
		        textAlign:'center',
		        text: titleText,
		        bottom:'16px',
		        textStyle:{
		        	fontWeight:'normal',
		        	fontSize:18,
		        	color:'#6197cc'
		        }
		    },
		    tooltip: {
		        trigger: 'axis',
		        formatter: function (params) {
		            params = params[0];
		            return params.name + params.seriesName + (params.value) + '%';
		        },
		        axisPointer: {
		            animation: false
		        }
		    },
		    xAxis : [
		        {
		            type : 'category',
		            boundaryGap : false,
		            axisLine: {onZero: true},
		            data: timeData
		        }
		    ],
		    yAxis : [
		        {
		            type : 'value',
		            max : 100.0,
		            mix:0.0
		        }
		    ],
		    series : [
		        {
		            name:seriesName,
		            type:'line',
		            symbolSize: 8,
		            hoverAnimation: false,
		            areaStyle: {
		                normal: {
		                    color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
		                    	 offset: 0,
		                         color: 'rgb(17, 125, 250)'
		                     }, {
		                         offset: 1,
		                         color: 'rgb(241, 246, 250)'
		                    }])
		                }
		            },
		            data:typeData
		            
		        }
		    ]
		};
		return option;
	}
	
	
	
	cpuChart.setOption(getOption('cpu'));
	ramChart.setOption(getOption('ram'));
	diskChart.setOption(getOption('disk'));
	netWorkChart.setOption(getOption('netWork'));
	
	setInterval(function () {
		getCpuUsage()

		cpuChart.setOption(getOption('cpu'));
		ramChart.setOption(getOption('ram'));
		diskChart.setOption(getOption('disk'));
		netWorkChart.setOption(getOption('netWork'));
	}, 5000);
	
	
})
/**
 * 重启语音转写引擎
 */
function doRestartAsr(){
	var auth=window.parent.getAuth();
	parent.$.messager.confirm('重启语音识别','您确定要重启语音识别引擎吗？',function(data){
		if(data){
			window.parent.showloading();
			$.ajax({
				type:'post',
				data:{authenticityToken:auth},
				url: $Path+'/admin/doRestartAsr',
				success:function(data){
					window.parent.hideloading();
					if(data.code==200){
						parent.$.messager.alert("成功","重启成功",'info');
					}
					else{
						parent.$.messager.alert("重启失败",data.msg,'error');
					}
				},
				error:function(data){
					window.parent.hideloading();
					parent.$.messager.alert("重启失败","服务器开小差了~",'error');
				}
			});
		}
	})
}
/**
 * 重启语音播报引擎
 */
function doRestartTts(){
	var auth=window.parent.getAuth();
	parent.$.messager.confirm('重启语音播报','您确定要重启语音播报引擎吗？',function(data){
		if(data){
			window.parent.showloading();
			$.ajax({
				type:'post',
				data:{authenticityToken:auth},
				url: $Path+'/admin/doRestartTts',
				success:function(data){
					window.parent.hideloading();
					if(data.code==200){
						parent.$.messager.alert("成功","重启成功",'info');
					}
					else{
						parent.$.messager.alert("重启失败",data.msg,'error');
					}
				},
				error:function(data){
					window.parent.hideloading();
					parent.$.messager.alert("重启失败","服务器开小差了~",'error');
				}
			});
		}
	})
}
/**
 * 重启服务
 */
function doReboot(){
	var auth=window.parent.getAuth();
	parent.$.messager.prompt('重启服务','请输入管理员密码',function(data){
		if(data){
			window.parent.showloading();
			$.ajax({
				type:'post',
				data:{password:data,authenticityToken:auth},
				url: $Path+'/admin/doReboot',
				success:function(data){
					window.parent.hideloading();
					if(data.code==200){
						parent.$.messager.alert("成功","重启成功",'info');
					}
					else{
						parent.$.messager.alert("重启失败",data.msg,'error');
					}
				},
				error:function(data){
					window.parent.hideloading();
					parent.$.messager.alert("重启失败","服务器开小差了~",'error');
				}
			});
		}
	})
}


